package com.my.basics.demo.demo02;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author mars
 * @date 2025-04-12 09:35
 */
@Slf4j
public class TimeClientHandler extends ChannelHandlerAdapter {

    /**
     *  连接建立时立即调用
     *
     * @param ctx ChannelHandlerContext
     * @throws Exception 异常
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String msg = "QUERY TIME";
        ctx.channel().writeAndFlush(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        String receiveMsg = buf.toString(CharsetUtil.UTF_8);
        log.info("Now is {}", receiveMsg);
    }
}
